package com.edu.dao.impl;

import com.edu.dao.SliderDao;
import com.edu.entity.SliderEntity;
import com.edu.utils.DatabaseManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * com.edu.dao.impl
 * 2024/7/5 10:20
 *
 * @author Fancy
 * @version 1.0
 */
public class SliderDaoImpl implements SliderDao {
    private Connection connection;

    public SliderDaoImpl() {
        try {
            this.connection = DatabaseManager.getInstance().getConnection();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public List<SliderEntity> getAllSlider() {
        try {
            String sql = "SELECT * FROM Slider";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            List<SliderEntity> sliderEntities = new LinkedList<>();
            while (resultSet.next()) {
                // Create and return User object
                SliderEntity sliderEntity = new SliderEntity(
                        resultSet.getInt("slider_id"),
                        resultSet.getString("image_url"),
                        resultSet.getString("title"),
                        resultSet.getString("description"),
                        resultSet.getString("link_url"),
                        resultSet.getInt("display_order"),
                        resultSet.getString("status")
                );
                sliderEntities.add(sliderEntity);
            }
            return sliderEntities;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateStatus(String newStatus, int sliderId) {
        String sql = "UPDATE Slider SET status = ? WHERE slider_id = ?";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, newStatus);
            statement.setInt(2, sliderId);
            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }

    }

    @Override
    public int deleteSlider(int sliderId) {
        String sql = "DELETE FROM Slider WHERE slider_id = ?";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1, sliderId);
            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }

    }

    @Override
    public int createSlider(SliderEntity slider) {
        String sql = "INSERT INTO Slider (image_url, title, description, link_url, display_order, status) VALUES (?, ?, ?, ?, ?, ?)";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, slider.getImageUrl());
            statement.setString(2, slider.getTitle());
            statement.setString(3, slider.getDescription());
            statement.setString(4, slider.getLinkUrl());
            statement.setInt(5, slider.getDisplayOrder());
            statement.setString(6, slider.getStatus());
            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }
}
